package com.tenement.common;

import com.tenement.enums.RespBusiness;
import lombok.Data;

import java.time.LocalDateTime;

@Data
public class Axios<T> {

    // 用于标识返回的状态码，200表示成功
    private int code;

    // 附加的消息描述，如成功提示、错误信息等
    private String msg;

    // 返回的数据
    private T data;

    // 请求的时间
    private LocalDateTime timestamp = LocalDateTime.now();



    public Axios(RespBusiness respBusiness) {
        this(respBusiness.getCode(), respBusiness.getMsg(), null);
        this.code = respBusiness.getCode();
        this.msg = respBusiness.getMsg();
    }

    public Axios(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    // 可以定义一些静态工厂方法来方便地创建成功的响应实例
    public static <T> Axios<T> success(T data) {
        return new Axios<>(200, "操作成功", data);
    }

    public static <T> Axios<T> error(RespBusiness respBusiness) {
        return new Axios<>(respBusiness.getCode(), respBusiness.getMsg(), null);
    }

    public static <T> Axios<T> error(RespBusiness respBusiness,T data) {
        return new Axios<>(respBusiness.getCode(), respBusiness.getMsg(), data);
    }


    public static <T> Axios<T> error(int code, String msg) {
        return new Axios<>(code,msg,null);
    }
}

